package com.ja.openchat.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentEditUserHomePageBinding;
import com.ja.openchat.ui.chat.group.ModifyFieldActivity;
import com.ja.openchat.viewmodels.EditHomePageViewModel;
import com.ja.openchat.views.AttachCopyPopupView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupPosition;
import com.zy.openchat.util.MediaUtils;

import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.KLog;

//编辑主页界面
public class EditUserHomePageFragment extends BaseFragment<FragmentEditUserHomePageBinding, EditHomePageViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_edit_user_home_page;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.mUC.getSelectPic().observe(this, unused -> {
            MediaUtils.choosePhotoFg(this, true);
        });
        viewModel.mUC.getType().observe(this, integer -> {
            switch (integer) {
                case 1:
                    //设置昵称
                    ModifyFieldActivity.start_setMyNickname(getActivity());
                    break;
                case 2:
                    //设置用户名
                    ModifyFieldActivity.start_setMyUsername(getActivity());
                    break;
                case 3:
                    ModifyFieldActivity.start_setMySignature(getActivity(), viewModel.userSignature.get());
                    break;
            }
        });
        viewModel.mUC.getShowPaymentPasswordDialogEvent().observe(this, maps -> {
            String content = (String) maps.get("content");
            if (!TextUtils.isEmpty(content)) {
                int type = (int) maps.get("type");
                View view = binding.tvNick;
                switch (type) {
                    case 1:
                        view = binding.tvNick;
                        break;
//                    case 2:
//                        view = binding.tvName;
//                        break;
//                    case 3:
//                        view = binding.tvPhone;
//                        break;
                }
                new XPopup.Builder(getActivity()).hasShadowBg(false).atView(view).popupPosition(PopupPosition.Top).asCustom(new AttachCopyPopupView(getActivity(), content)).show();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.loadFullInfo();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && data != null) {
            String country = data.getStringExtra("country");
            KLog.d(country);
            viewModel.setCountry(country);
        } else {
            List<String> photos = MediaUtils.getResultAlbumData(getActivity(), requestCode, resultCode, data);
            if (photos != null && photos.size() > 0) {
                viewModel.changeMyPhoto(photos.get(0));
            }
        }
    }

}